<?php

class base_layout {

    /*-READ-ONLY PROPERTIES----------------------------------------------------------------------*/
    protected $_elementID; public function getID() { return $this->_elementID; }

    protected $_css = array(); public function getCssLinks() { return $this->_css; }
    protected $_js = array(); public function getJsLinks() { return $this->_js; }
    protected $_smartyVars = array(); public function getSmartyVars() { return $this->_smartyVars; }

    protected $_tpl_list = array(0 => '');
    public function getTplFile() { return $this->_tpl_list[0]; }
    public function getTplList() { return $this->_tpl_list; }
    /*-END READ-ONLY PROPERTIES------------------------------------------------------------------*/

    /** CONSTRUCTOR METHOD
     * @param string $TPLfile: path to tpl file
     * @param string|array $cssLink: contains multiple or single link(s) to css
     * @param string|array $jsLink: contains multiple or single link(s) to js
     * @param array $smartyVars: array contains initial smarty variables ($key => $value form)
     */
    public function __construct($TPLfile = null, $cssLink = null, $jsLink = null, $smartyVars = null) {
        if (is_string($TPLfile)) $this->_tpl_list[0] = $TPLfile;
        else $this->_tpl_list[0] = strval($TPLfile);

        $this->_elementID = self::encode(date("YmdHis"));

        if (is_array($cssLink)) { foreach ($cssLink as $item) $this->_css[] = trim($item); }
        elseif (is_string($cssLink)) $this->_css[] = trim($cssLink);

        if (is_array($jsLink)) { foreach ($jsLink as $item) $this->_js[] = trim($item); }
        elseif (is_string($jsLink)) $this->_js[] = trim($jsLink);

        if (is_array($smartyVars))
            foreach ($smartyVars as $key => $value) $this->_smartyVars[$key] = $value;
    }

    /** add a single or multiple css link to this layout
     * @param string|array $cssLink: contains multiple or single link(s) to css
     */
    public function addCss($cssLink) {
        if (is_array($cssLink)) { foreach ($cssLink as $item) $this->_css[] = trim($item); }
        elseif (is_string($cssLink)) $this->_css[] = trim($cssLink);
        $this->_css = array_unique($this->_css);
    }

    /** add a single or multiple js link to this layout
     * @param string|array $cssLink: contains multiple or single link(s) to js
     */
    public function addJs($jsLink) {
        if (is_array($jsLink)) { foreach ($jsLink as $item) $this->_js[] = trim($item); }
        elseif (is_string($jsLink)) $this->_js[] = trim($jsLink);
        $this->_js = array_unique($this->_js);
    }

    /** add smarty variables to this layout
     * @param array $smartyVars: array contains initial smarty variables ($key => $value form)
     */
    public function addSmartyVars($smartyVars) {
        if (is_array($smartyVars))
            foreach ($smartyVars as $key => $value) $this->_smartyVars[$key] = $value;
    }

    /** add a child layout to this layout
     * @param base_layout $childLayout: another base_layout object that will be added as child of this layout
     * @param bool $getDescendentTpl: include all the descendents of this child to my child's tpl (default is false)
     */
    public function addLayout($childLayout, $getDescendentTpl = false) {
        $childID = 0;

        // add the childLayout's tpl file to _tpl_list array of this object
        if (trim($childLayout->getTplFile()) != '') { // if this child layout is not an abstract layout, then add to layout list
            $this->_tpl_list[] = array($childLayout->getTplFile(), 0, $this->getTplFile());
            $childID = count($this->_tpl_list) - 1;
        } else $getDescendentTpl = true; // if this child layout is an abstract layout, then we have to include all descendents of this child

        // add children-tpl-file of the childLayout to _tpl_list array of this object
        if ($getDescendentTpl) {
            $childTplList = $childLayout->getTplList();
            foreach ($childTplList as $index=>$childTplItem)
            	if ($index > 0) {
	                $childTplItem[1] += $childID;
	                $childTplItem[2] = (trim($childLayout->getTplFile()) != '' ? $childLayout->getTplFile() : $this->getTplFile());
	                $this->_tpl_list[] = $childTplItem;
            	}
        }

        $this->_css = array_unique(array_merge($this->_css, $childLayout->getCssLinks()));
        $this->_js = array_unique(array_merge($this->_js, $childLayout->getJsLinks()));
        $this->_smartyVars = array_merge($this->_smartyVars, $childLayout->getSmartyVars());
    }

    private static function encode($str) { for($i = 0; $i < 3; $i++) $str = strrev(base64_encode($str)); return $str; }
    private static function decode($str) { for($i = 0; $i < 3; $i++) $str = base64_decode(strrev($str)); return $str; }
}

?>